//
//  NSObject+BZJSONSerialization.m
//  BZFoundation
//
//  Created by 郭艺伟 on 14-10-30.
//  Copyright (c) 2014年 ND. All rights reserved.
//

#import "NSObject+BZJSONSerialization.h"

@implementation NSObject (BZJSONSerialization)
- (BOOL)bz_isValidJSONObject {
    return [NSJSONSerialization isValidJSONObject:self];
}

- (NSString *)bz_jsonRepresentation {
    return [self bz_jsonRepresentationWithError:nil];
}

- (NSString *)bz_jsonRepresentationWithError:(NSError * __autoreleasing *)error {
    return [self bz_jsonRepresentationWithOptions:NSJSONWritingPrettyPrinted error:error];
}

- (NSString *)bz_jsonRepresentationWithOptions:(NSJSONWritingOptions)options error:(NSError * __autoreleasing *)error {
    if ([self bz_isValidJSONObject]) {
        NSError *errorT = nil;
        NSData *data = [NSJSONSerialization dataWithJSONObject:self options:options error:&errorT];
        if (errorT) {
            if (error) {
                *error = errorT;
            }
            return nil;
        }
        NSString *strJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        if (!strJson) {
            return nil;
        }
        
        return strJson;
    }
    return nil;
}
@end
